package com.zhhtech.zhh.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.zhhtech.zhh.R
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.FirmException
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.CaseOrderBean
import com.zhhtech.zhh.databinding.ActivityMyConsultationBinding
import com.zhhtech.zhh.databinding.ItemMyConsultationBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseFragment
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.dialog.SchemeResultDialog
import com.zhhtech.zhh.ui.ext.toastMessageLong
import com.zhhtech.zhh.ui.ext.value
import com.zhhtech.zhh.ui.helper.InjectBundle
import com.zhhtech.zhh.ui.helper.RefreshHelper
import com.zhhtech.zhh.ui.helper.jumpWx
import com.zhhtech.zhh.ui.helper.openWorkKf

class MyConsultationFragment : BaseFragment<ActivityMyConsultationBinding>() {
    private val home: Boolean by InjectBundle()
    private lateinit var mRefreshHelper: RefreshHelper<CaseOrderBean>
    private val mAdapter = MyConsultationAdapter()
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        vb.titleBar.isShowLeft = !home
        mAdapter.setOnClickAdapterListenter(::onClick)
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvList, mAdapter, listener = ::onReload)
        vb.refreshLayout.setEnableLoadMore(false)
        vb.refreshLayout.post {
            vb.refreshLayout.setPrimaryColorsId(android.R.color.transparent, R.color.white)
        }
        mRefreshHelper.onRefresh()
    }

    private fun onReload() {
        launchUI({ api.orderList().toData()?.list }, mRefreshHelper.handApiObserver())
    }

    private fun onClick(view: View, position: Int) {
        when (view.id) {
            R.id.button_cancel -> {
                launchUI({ jumpWx(api.indexConfig().toData()) }, ApiObserver(requireContext()))
            }
        }
    }
}

class MyConsultationAdapter : BaseRecyclerAdapter<ItemMyConsultationBinding, CaseOrderBean>() {
    override fun onBindViewHolder(
        vb: ItemMyConsultationBinding,
        bean: CaseOrderBean,
        position: Int
    ) {
        vb.tv1.text = bean.loanType
        vb.tv2.text = bean.amount
        vb.tv3.text = bean.statusText
        vb.tv4.text = bean.createAt
        vb.tv5.text = bean.statusText
        // 状态 1取消 100完成 其他都是进行中
        when (bean.orderStatus) {
            "1" -> {
                vb.tv5.setBackgroundResource(R.drawable.status_cancel)
                vb.tv5.setTextColor(vb.tv5.context.getColor(R.color.text_color))
                vb.buttonCancel.isVisible = false
            }

            "100" -> {
                vb.tv5.setBackgroundResource(R.drawable.status_complete)
                vb.tv5.setTextColor(vb.tv5.context.getColor(R.color.text_color))
                vb.buttonCancel.isVisible = false
            }

            else -> {
                vb.tv5.setBackgroundResource(R.drawable.status_in)
                vb.tv5.setTextColor(vb.tv5.context.getColor(R.color.white))
                vb.buttonCancel.isVisible = true
            }
        }
        setViewClick(position, vb.buttonCancel)
    }
}